package com.zat.mes.andrmes.customerview

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.customerview.adapter.BottomDialogAdapter
import com.zat.mes.andrmes.data.ParantTypeItem
import kotlinx.android.synthetic.main.layout_dialog_bottom.view.*

/**
 * 通用底部弹框
 */
class BottomDailog : Dialog {


    constructor(
        context: Context,
        list: MutableList<ParantTypeItem>?,
        selectList:MutableList<ParantTypeItem>,
        listener: OnBottomSelectListener
    ) : super(context, R.style.MyDialog) {
        val contentView = LayoutInflater.from(context).inflate(R.layout.layout_dialog_bottom, null)
        contentView.ryDialog.layoutManager = LinearLayoutManager(context)
        val adapter = BottomDialogAdapter(R.layout.item_dialog_bottom)
        contentView.ryDialog.adapter = adapter
        list?.let { adapter.addData(it) }
        adapter.selectList =selectList
        adapter.notifyDataSetChanged()
        setContentView(contentView)
        val layoutParams = window?.getAttributes()
        layoutParams?.width = WindowManager.LayoutParams.MATCH_PARENT
        window?.attributes = layoutParams
        window?.setGravity(Gravity.BOTTOM)
        setCanceledOnTouchOutside(true)
        show()
        contentView.btnCancelBottomDialog.setOnClickListener { dismiss() }
        contentView.btnOkBottomDialog.setOnClickListener {
            if (listener != null) {
                listener.onBottomSelect(adapter.selectList)
            }
            dismiss()
        }
    }


    interface OnBottomSelectListener {
        fun onBottomSelect(list: MutableList<ParantTypeItem>)
    }

}